/*  Adjust GREP-found text size by X (pt)
    InDesign JSX
*/
(function () {
    if (app.documents.length === 0) { alert("无打开文档"); return; }

    app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "GREP 字号批量增量");

    function main() {
        var doc = app.activeDocument;

        // 读取可用 GREP 预设名
        var presetNames;
        try {
            presetNames = app.findChangeGrepQueryNames; // CS6+
        } catch (e) {
            presetNames = [];
        }
        if (!presetNames || presetNames.length === 0) {
            alert("未检测到已保存的 GREP 预设。请先在“查找/更改”面板保存一个 GREP 预设。");
            return;
        }

        // 对话框：选择预设，输入 X
        var dlg = new Window("dialog", "GREP 字号增量");
        var g1 = dlg.add("group");
        g1.add("statictext", undefined, "GREP 预设：");
        var dd = g1.add("dropdownlist", undefined, presetNames);
        dd.selection = 0;

        var g2 = dlg.add("group");
        g2.add("statictext", undefined, "增量 X（pt，可正负小数）：");
        var xEdit = g2.add("edittext", undefined, "1"); xEdit.characters = 10;

        var btns = dlg.add("group");
        btns.alignment = "right";
        btns.add("button", undefined, "确定", {name: "ok"});
        btns.add("button", undefined, "取消", {name: "cancel"});

        if (dlg.show() !== 1) return;

        var presetName = String(dd.selection.text);
        var delta = parseFloat(String(xEdit.text).replace(",", "."));
        if (isNaN(delta)) { alert("X 非法"); return; }

        // 备份并清空查找/更改首选项
        var oldFind = app.findGrepPreferences.properties;
        var oldChange = app.changeGrepPreferences.properties;
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;

        try {
            // 载入 GREP 预设，获得其查找条件
            app.loadFindChangeQuery(presetName, SearchModes.grepSearch);

            // 执行查找
            var found = doc.findGrep();
            if (!found || found.length === 0) { alert("未找到匹配文本"); return; }

            // 对命中范围内的每个字符调整字号
            // 说明：必须逐字符处理，因字号可能混合；changeGrep 不支持相对增量
            var minPt = 0.1;
            for (var i = 0; i < found.length; i++) {
                var chs = found[i].characters;
                // getElements() 可减少跨进程调用
                var arr = chs.getElements();
                for (var j = 0; j < arr.length; j++) {
                    var sz = arr[j].pointSize;
                    if (typeof sz === "number" && isFinite(sz)) {
                        var ns = sz + delta;
                        if (ns < minPt) ns = minPt;
                        arr[j].pointSize = ns;
                    }
                }
            }
        } catch (err) {
            alert("执行出错：\n" + err);
        } finally {
            // 还原查找/更改首选项
            try { app.findGrepPreferences = NothingEnum.NOTHING; app.findGrepPreferences.properties = oldFind; } catch (_) {}
            try { app.changeGrepPreferences = NothingEnum.NOTHING; app.changeGrepPreferences.properties = oldChange; } catch (_) {}
        }
    }
})();
